/*
 * Copyright © 2024-2025 Wenze Wei
 *
 * This file is part of StadionOS.
 *
 * This Source Code Form is subject to the terms of the GNU General Public License, v. 2.0.
 * You can obtain a copy of the GPL at
 * https://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 */

#include "sapi.h"
#include "../compositor.h"
#include "../input.h"

static struct onatu_compositor *s_compositor = NULL;

static void sapi_create_window(int width, int height) {
    struct onatu_window *window = calloc(1, sizeof(*window));
    window->width = width;
    window->height = height;
    wl_list_insert(&s_compositor->windows, &window->link);
}

static void draw_rounded_rect(int x, int y, int w, int h, int radius) {
    struct onatu_surface *surf = compositor_get_current_surface();
    if(surf) {
        surf->ops->draw_rounded_rect(surf, x, y, w, h, radius);
    }
}

static void draw_text(int x, int y, const char* text) {
    struct onatu_surface *surf = compositor_get_current_surface();
    if(surf) {
        surf->ops->draw_text(surf, x, y, text);
    }
}

static void sapi_register_key_handler(void (*handler)(int, int)) {
    s_compositor->key_handler = handler;
}

static void sapi_init(SAPI *api) {
    memset(api, 0, sizeof(SAPI));
    
    api->create_window = sapi_create_window;
    api->move_window = compositor_move_window;
    api->set_window_title = compositor_set_title;
    api->register_key_handler = sapi_register_key_handler;
    api->begin_frame = compositor_begin_frame;
    api->end_frame = compositor_end_frame;
    api->draw_rounded_rect = draw_rounded_rect;
    api->draw_text = draw_text;
    
    s_compositor = compositor_get_instance();
}
